/*
 * Placement_new_Pool.h
 *
 *  Created on: 2014. 6. 23.
 *      Author: jjaehuny
 */

#ifndef FRAMEWORK_MEMORY_PLACEMENT_NEW_POOL_H_
#define FRAMEWORK_MEMORY_PLACEMENT_NEW_POOL_H_

#include "Base_Pool.h"

namespace Framework { namespace Memory {

template<class Type>
class Placement_new_Pool : public Base_Pool<Type>
{
public:
	Placement_new_Pool (bool bPost = false, unsigned long pool_size = 1);
	virtual ~Placement_new_Pool ();

protected:
	virtual Type * allocator ();
	virtual void deleter (Type * pType);

};

template<class Type>
Placement_new_Pool<Type>::Placement_new_Pool (bool bPost, unsigned long pool_size) : Base_Pool<Type>(bPost, pool_size)
{

}

template<class Type>
Placement_new_Pool<Type>::~Placement_new_Pool ()
{

}

template<class Type> Type *
Placement_new_Pool<Type>::allocator ()
{
	Type * pType = NULL;
	{
		if (true != Base_Pool<Type>::type_deque_.empty())
		{
			pType = Base_Pool<Type>::type_deque_.front();
			Base_Pool<Type>::type_deque_.pop_front();

			// placement new
			if (NULL != pType)
			{
				new (pType) Type;
			}
		}
		else
		{
			pType = new Type;
		}
	}

	return pType;
}

template<class Type> void
Placement_new_Pool<Type>::deleter (Type * pType)
{
	if (NULL != pType)
	{
		// placement delete
		pType->~Type();

		Base_Pool<Type>::type_deque_.push_back(pType);
	}
}

} /*Memory*/ } /*Framework*/

#endif /* FRAMEWORK_MEMORY_PLACEMENT_NEW_POOL_H_ */
